﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcBlog.Common.Models;
using MvcBlog.ViewModels;

namespace MvcBlog.Controllers
{
    public class EntryController : Controller
    {
        EntryDataAccess entryDB;
        CommentDataAccess commentDB;
        LoginDataAccess loginDB;

        public EntryController()
        {
            var con = ConnectionFactory.getConnection();

            entryDB = new EntryDataAccess(con);
            commentDB = new CommentDataAccess(con);
            loginDB = new LoginDataAccess(con);
        }

        //
        // GET: /Entry/

        public ActionResult Index()
        {
            return RedirectToAction("/");
        }

        //
        // GET: /Entry/Details/5

        public ActionResult Details(int id)
        {

            try
            {
                Entry e = entryDB.getEntryByID(id);
                
                List<Comment> ListOfComments = commentDB.getCommentsByEntryID(e.Entry_ID);
                EntryViewModel viewModel = new EntryViewModel(e, ListOfComments);

                return View(viewModel);
            }
            catch (NullReferenceException)
            {
                return View("Error");
                //return RedirectToAction("../Home/Index");
            }


        }

        //
        // GET: /Entry/Create

        public ActionResult Create()
        {
            //User Objekt schon beim Einloggen in den Cache legen, damit wir nicht wieder über die db müssen.

            bool allowed = Permissions.Allowed(HttpContext.User.Identity.Name, DbConstants.ROLE_AUTHOR);
            if (allowed)
            {
                return View();
            }
            else
            {
                return View("NotAuthorized");
            }

        } 

        //
        // POST: /Entry/Create

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(EntryViewModel entryVm)
        {
            if (ModelState.IsValid)
            {
                try
                {

                    //berechtigungen prüfen
                    bool allowed = Permissions.Allowed(HttpContext.User.Identity.Name, DbConstants.ROLE_AUTHOR);
                    if (!allowed)
                    {
                        return View("NotAuthorized");
                    }

                    Entry newEntry = new Entry();
                    entryVm.ApplyChanges(newEntry);

                    Entry addedEntry = new Entry();
                    User user = CacheController.getUser(HttpContext.User.Identity.Name);
                    addedEntry = entryDB.insertEntry(newEntry, user);

                    return RedirectToAction("Details/" + addedEntry.Entry_ID);
                    
                }
                catch (Exception)
                {
                    //Datenbankfehler
                    return View("Error");
                }
            }
            else
            {
                //Fehler bei der Usereingabe
                return View();
            }

        }

        //AJAX Methode
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Comment(EntryViewModel entryVm)
        {

            if (entryVm.newComment == null || String.IsNullOrEmpty(entryVm.newComment.Trim()))
            {
                return View();
            }

            try
            {
                //wer ist eingeloggt? -> user objekt aus dem cache
                User user = CacheController.getUser(HttpContext.User.Identity.Name);

                //neues kommentar erzeugen und in die db schreiben
                 //zuerst berechtigungen prüfen
                bool allowed = Permissions.Allowed(HttpContext.User.Identity.Name);
                if (allowed)
                {
                    Comment newComment = new Comment();
                    newComment.Body = entryVm.newComment.Trim();
                    commentDB.insertComment(newComment, entryVm.Entry_ID, user);
                }
                
                List<Comment> ListOfComments = commentDB.getCommentsByEntryID(entryVm.Entry_ID);
                EntryViewModel viewModel = new EntryViewModel(new Entry(), ListOfComments);

                return PartialView("CommentList", viewModel);
            }
            catch(Exception)
            { return View("Error"); }

            
        }

        /********************************
         * 
         * 
        //
        // GET: /Entry/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Entry/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Entry/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Entry/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
         * 
         * *********************/
    }
}
